package com.intel.two.controller.medical;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;

import com.intel.two.bo.medical.ArmariumBo;
import com.intel.two.common.LayuiPageVo;
import com.intel.two.common.R;
import com.intel.two.listener.medical.WebArmariumBoListener;

import com.intel.two.pojo.medical.Armarium;
import com.intel.two.service.medical.ArmariumService;
import com.intel.two.utils.OssUtil;
import com.intel.two.vo.medical.ArmariumVo;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import java.util.List;
import java.util.Map;

/**
 * @Author:xiaofeia
 * @DATE:2022/10/13
 */
@RestController
@RequestMapping("/medical/armarium")
public class ArmariumContorller {
    @Autowired
    private ArmariumService armariumService;


    @RequestMapping("/list")
    public LayuiPageVo<Armarium> armariumList(@RequestParam Map<String, Object> params){
//        System.out.println(params);
        return armariumService.armariumPageData(params);
    }

    //添加设备信息
    @PostMapping("/add")
    public R addArmarium(@RequestBody Armarium armarium){
//        System.out.println("armarium测试："+armarium);
        int result = armariumService.addArmarium(armarium);

        if(result==1){
            return R.ok();
        }else{
            return  R.error();
        }
    }
    //上传图片
    @PostMapping("/upload")
    public R uploadPic(MultipartFile file) throws Exception {
        //得到文件的名字
        String originalFilename = file.getOriginalFilename();

        String fileUrl = OssUtil.updateLoad(file.getInputStream(), originalFilename);
        // 上传成功之后的文件地址 写给前端

        return R.ok().data("url",fileUrl);

    }
    @GetMapping("/delete")
    public R deleteArmarium(Integer armarium_id){
        //根据hid删除房屋信息
//        System.out.println("armarium_id" + armarium_id);
        int result = armariumService.deleteArmariumById(armarium_id);
        if(result ==1){
            return R.ok().message("删除成功");
        }
        return R.error();
    }
    @PostMapping("/deleteIds")
    public R deletIdsArmarium(String armarium_ids){
//        System.out.println(armarium_ids);
        int result = armariumService.deletArmariumIds(armarium_ids);
        if(result > 0){
            return R.ok().message("删除选中成功");
        }
        return R.error();

    }
    @PostMapping("/edit")
    public R updateArmarium(Armarium armarium){
        System.out.println(armarium);
        int result = armariumService.updateArmarium(armarium);
        if(result == 1){
            return R.ok();
        }else {
            return R.error();
        }
    }
    //柱状图展示信息
    @RequestMapping("/echarBar")
    public R echarBar(){
        List<ArmariumVo> list = armariumService.amountBarData();
        System.out.println(list.toString());
        return R.ok().data("amountBar",list);
    }
    //



    @Autowired
    private WebArmariumBoListener webArmariumBoListener;

    //读取excel文件  把excel 文件中的数据存入数据库
    @RequestMapping("/import")
    public R importExcel(MultipartFile file) throws IOException {
        System.out.println("qqqq");
        /**
         * 参数1  文件流
         * 参数2 读取到的数据封装到哪个类型
         * 参数3 监听器  每读取一行数据 都会调用监听器的 invoke 方法
         *
         * */
        ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), ArmariumBo.class, webArmariumBoListener);
        //得到 读取的sheet页对象
        ExcelReaderSheetBuilder sheet = read.sheet();
        //读取sheet
        sheet.doRead();

        return R.ok();
    }

    @RequestMapping("/export")
    public void export(Armarium armarium, HttpServletResponse response) throws IOException {
        System.out.println("输出excel文件");
        //下载 excel文件 设置响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //防止中文乱码
        String filename = URLEncoder.encode("医疗设备信息导出","utf-8");
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        // 获取输出流
        ServletOutputStream outputStream = response.getOutputStream();

        // easyexcel 写数据到 输出流
        ExcelWriterBuilder write = EasyExcel.write(outputStream, ArmariumBo.class);
        ExcelWriterSheetBuilder sheet = write.sheet();

        //列表数据
        List<ArmariumBo> armariumExcelData =armariumService.armariumExcelData(armarium);
        sheet.doWrite(armariumExcelData);
    }

    //templetdownload
    @RequestMapping("/templetDownload")
    public void templetdownload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");

        String realpath = request.getServletContext().getRealPath("/template/医疗设备信息模板.xlsx" );

//        System.out.println(realpath);//确定模板放在哪里

        //根据文件的真实路径，得到文件对象
        File file = new File(realpath);
        //将文件对象转为字节
        byte[] bytes = FileUtils.readFileToByteArray(file);
        //防止中文乱码
        String filename = URLEncoder.encode("医疗设备信息模板","utf-8");

        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        //得到指向浏览器的输出流
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(bytes);
        outputStream.close();



    }

}
